package com.projectsquirrel;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import android.widget.TextView;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class NewObservation7 extends Activity  {
	
	private static final int PREFERENCES_GROUP_ID = 11;
	private static final int PAGE_2 = 12;
	private static final int PAGE_3 = 13;
	private static final int PAGE_4 = 14;
	private static final int PAGE_5 = 15;
	private static final int PAGE_6 = 16;
	private static final int PAGE_7 = 17;
	
	private LocationManager myManager;

	private Observation newObs;
	    

    private Button nextObsButton7;
    private TextView completitionStatus;
    private Context context;

    private RadioGroup dog_Group;
    private RadioGroup cat_Group;
    private RadioGroup coyote_Group;
    private RadioGroup hawk_Group;
    private RadioGroup grain_Group;
    private SharedPreferences pref;
    private CheckBox check1; 
    private CheckBox check2; 
    private CheckBox check3; 
    private CheckBox check4; 
    private CheckBox check5; 
    private CheckBox check6; 
    private CheckBox check7; 
    private CheckBox check8; 

    @Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.observationformfd);//layout 1.5
	    ImageButton infoIconButton = (ImageButton) findViewById(R.id.infoScreenIcon);
	    ImageButton settingsIconButton = (ImageButton) findViewById(R.id.settingsScreenIcon);
	    context = getApplicationContext();
	    nextObsButton7 = (Button) findViewById(R.id.nextObsScreen7);
	    pref = getSharedPreferences("AppSettings",MODE_PRIVATE);
	    //setup the gps manager
	    myManager = (LocationManager) getSystemService(LOCATION_SERVICE);	
	    String email = pref.getString("userEmail", null);
	    newObs = Observation.getInstance(email);
	    
	    
	    dog_Group = (RadioGroup) findViewById(R.id.dog_Group);
	    cat_Group = (RadioGroup) findViewById(R.id.cat_Group);
	    coyote_Group = (RadioGroup) findViewById(R.id.coyote_Group);
	    hawk_Group = (RadioGroup) findViewById(R.id.hawk_Group);
	    grain_Group = (RadioGroup) findViewById(R.id.grain_Group);
	    check1 = (CheckBox) findViewById(R.id.checkbox1);
	    check2 = (CheckBox) findViewById(R.id.checkbox2);
	    check3 = (CheckBox) findViewById(R.id.checkbox3);
	    check4 = (CheckBox) findViewById(R.id.checkbox4);
	    check5 = (CheckBox) findViewById(R.id.checkbox5);
	    check6 = (CheckBox) findViewById(R.id.checkbox6);
	    check7 = (CheckBox) findViewById(R.id.checkbox7);
	    check8 = (CheckBox) findViewById(R.id.checkbox8);
	    
	    String dog = newObs.getNumOfSiteDogs();
	    if(dog.equals("NONE")) dog_Group.check(R.id.dogs_None);
	    if(dog.equals("LOW")) dog_Group.check(R.id.dogs_Low);
	    if(dog.equals("MEDIUM")) dog_Group.check(R.id.dogs_Medium);
	    if(dog.equals("HIGH")) dog_Group.check(R.id.dogs_High);
	    String cat = newObs.getNumOfSiteCats();
	    if(cat.equals("NONE")) cat_Group.check(R.id.cats_None);
	    if(cat.equals("LOW")) cat_Group.check(R.id.cats_Low);
	    if(cat.equals("MEDIUM")) cat_Group.check(R.id.cats_Medium);
	    if(cat.equals("HIGH")) cat_Group.check(R.id.cats_High);
	    String coyote = newObs.getNumOfSiteCoyotes();
	    if(coyote.equals("NONE")) coyote_Group.check(R.id.coyotes_None);
	    if(coyote.equals("LOW")) coyote_Group.check(R.id.coyotes_Low);
	    if(coyote.equals("MEDIUM")) coyote_Group.check(R.id.coyotes_Medium);
	    if(coyote.equals("HIGH")) coyote_Group.check(R.id.coyotes_High);
	    String hawk = newObs.getNumOfSiteHawks();
	    if(hawk.equals("NONE")) hawk_Group.check(R.id.hawks_None);
	    if(hawk.equals("LOW")) hawk_Group.check(R.id.hawks_Low);
	    if(hawk.equals("MEDIUM")) hawk_Group.check(R.id.hawks_Medium);
	    if(hawk.equals("HIGH")) hawk_Group.check(R.id.hawks_High);
	    String grain = newObs.getNumOfSitePGrain();
	    if(grain.equals("NONE")) grain_Group.check(R.id.grains_None);
	    if(grain.equals("LOW")) grain_Group.check(R.id.grains_Low);
	    if(grain.equals("MEDIUM")) grain_Group.check(R.id.grains_Medium);
	    if(grain.equals("HIGH")) grain_Group.check(R.id.grains_High);

	    /* This does not update Observation.java, if you want a default entry you must also enter it in the class manually*/
/*	    ((RadioButton) findViewById(R.id.dogs_None)).performClick();
	    ((RadioButton) findViewById(R.id.cats_None)).performClick();
	    ((RadioButton) findViewById(R.id.hawks_None)).performClick();
	    ((RadioButton) findViewById(R.id.coyotes_None)).performClick();
	    ((RadioButton) findViewById(R.id.grains_None)).performClick();
	    //Like So
	    
	    newObs.setDogs("NONE");
	    newObs.setCats("NONE");
	    newObs.setCoyotes("NONE");
	    newObs.setHawks("NONE");
	    newObs.setPGrain("NONE");
	  */  
	    
	    completitionStatus = (TextView) findViewById(R.id.textView2);
	    completitionStatus.setText("100% complete");
	    
	   
	    check1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
	    {
	        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
	        {
	        	if(isChecked)
	        	{
	        	    ((RadioButton) findViewById(R.id.dogs_None)).setChecked(true);
	        	    ((RadioButton) findViewById(R.id.cats_None)).setChecked(true);
	             	check2.setChecked(false);	    	        		
		        	check3.setChecked(false);	    	        		
		        	check4.setChecked(false);	    	        		
	        	}
	        	else
	        	{
	           		if(dog_Group.getCheckedRadioButtonId()==R.id.dogs_None)
	        		{
	        	    dog_Group.clearCheck();
	        		}
	        		if(cat_Group.getCheckedRadioButtonId()==R.id.cats_None)
	        		{
	        			cat_Group.clearCheck();
	        		}
		    	}
	        }
	    });

	    check2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
	    {
	        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
	        {
	        	if(isChecked)
	        	{
	        	    ((RadioButton) findViewById(R.id.dogs_Low)).setChecked(true);
	        	    ((RadioButton) findViewById(R.id.cats_Low)).setChecked(true);
	             	check1.setChecked(false);	    	        		
		        	check3.setChecked(false);	    	        		
		        	check4.setChecked(false);	    	        		
	        	}
	        	else
	        	{
	           		if(dog_Group.getCheckedRadioButtonId()==R.id.dogs_Low)
	        		{
	        	    dog_Group.clearCheck();
	        		}
	        		if(cat_Group.getCheckedRadioButtonId()==R.id.cats_Low)
	        		{
	        			cat_Group.clearCheck();
	        		}
		    	}
	        }
	    });

	    check3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
	    {
	        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
	        {
	        	if(isChecked)
	        	{
	        	    ((RadioButton) findViewById(R.id.dogs_Medium)).setChecked(true);
	        	    ((RadioButton) findViewById(R.id.cats_Medium)).setChecked(true);
	             	check2.setChecked(false);	    	        		
		        	check1.setChecked(false);	    	        		
		        	check4.setChecked(false);	    	        		
	        	}
	        	else
	        	{
	           		if(dog_Group.getCheckedRadioButtonId()==R.id.dogs_Medium)
	        		{
	        	    dog_Group.clearCheck();
	        		}
	        		if(cat_Group.getCheckedRadioButtonId()==R.id.cats_Medium)
	        		{
	        			cat_Group.clearCheck();
	        		}
		    	}
	        }
	    });

	    check4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
	    {
	        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
	        {
	        	if(isChecked)
	        	{
	        	    ((RadioButton) findViewById(R.id.dogs_High)).setChecked(true);
	        	    ((RadioButton) findViewById(R.id.cats_High)).setChecked(true);
	                check2.setChecked(false);	    	        		
		        	check3.setChecked(false);	    	        		
		        	check1.setChecked(false);	    	        		
	        	}
	        	else
	        	{
	           		if(dog_Group.getCheckedRadioButtonId()==R.id.dogs_High)
	        		{
	        	    dog_Group.clearCheck();
	        		}
	        		if(cat_Group.getCheckedRadioButtonId()==R.id.cats_High)
	        		{
	        			cat_Group.clearCheck();
	        		}
		    	}
	        }
	    });

	    check5.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
	    {
	        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
	        {
	        	if(isChecked)
	        	{
	        	    ((RadioButton) findViewById(R.id.hawks_None)).setChecked(true);
	        	    ((RadioButton) findViewById(R.id.coyotes_None)).setChecked(true);
	        	    ((RadioButton) findViewById(R.id.grains_None)).setChecked(true);
	             	check6.setChecked(false);	    	        		
		        	check7.setChecked(false);	    	        		
		        	check8.setChecked(false);	    	        		
	        	}
	        	else
	        	{
	           		if(coyote_Group.getCheckedRadioButtonId()==R.id.coyotes_None)
	        		{
	        	    coyote_Group.clearCheck();
	        		}
	        		if(hawk_Group.getCheckedRadioButtonId()==R.id.hawks_None)
	        		{
	        			hawk_Group.clearCheck();
	        		}
	        		if(grain_Group.getCheckedRadioButtonId()==R.id.grains_None)
	        		{
	        			grain_Group.clearCheck();
	        		}
		    	}
	        }
	    });

	    check6.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
	    {
	        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
	        {
	        	if(isChecked)
	        	{
	        	    ((RadioButton) findViewById(R.id.hawks_Low)).setChecked(true);
	        	    ((RadioButton) findViewById(R.id.coyotes_Low)).setChecked(true);
	        	    ((RadioButton) findViewById(R.id.grains_Low)).setChecked(true);
	             	check5.setChecked(false);	    	        		
		        	check7.setChecked(false);	    	        		
		        	check8.setChecked(false);	    	        		
	        	}
	        	else
	        	{
	           		if(coyote_Group.getCheckedRadioButtonId()==R.id.coyotes_Low)
	        		{
	        	    coyote_Group.clearCheck();
	        		}
	        		if(hawk_Group.getCheckedRadioButtonId()==R.id.hawks_Low)
	        		{
	        			hawk_Group.clearCheck();
	        		}
	        		if(grain_Group.getCheckedRadioButtonId()==R.id.grains_Low)
	        		{
	        			grain_Group.clearCheck();
	        		}
		    	}
	        }
	    });

	    check7.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
	    {
	        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
	        {
	        	if(isChecked)
	        	{
	        	    ((RadioButton) findViewById(R.id.hawks_Medium)).setChecked(true);
	        	    ((RadioButton) findViewById(R.id.coyotes_Medium)).setChecked(true);
	        	    ((RadioButton) findViewById(R.id.grains_Medium)).setChecked(true);
	             	check5.setChecked(false);	    	        		
		         	check6.setChecked(false);	    	        		
		        	check8.setChecked(false);	    	        		
	        	}
	        	else
	        	{
	           		if(coyote_Group.getCheckedRadioButtonId()==R.id.coyotes_Medium)
	        		{
	        	    coyote_Group.clearCheck();
	        		}
	        		if(hawk_Group.getCheckedRadioButtonId()==R.id.hawks_Medium)
	        		{
	        			hawk_Group.clearCheck();
	        		}
	        		if(grain_Group.getCheckedRadioButtonId()==R.id.grains_Medium)
	        		{
	        			grain_Group.clearCheck();
	        		}
		    	}
	        }
	    });

	    check8.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
	    {
	        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
	        {
	        	if(isChecked)
	        	{
	        	    ((RadioButton) findViewById(R.id.hawks_High)).setChecked(true);
	        	    ((RadioButton) findViewById(R.id.coyotes_High)).setChecked(true);
	        	    ((RadioButton) findViewById(R.id.grains_High)).setChecked(true);
	             	check5.setChecked(false);	    	        		
		        	check6.setChecked(false);	    	        		
		            check7.setChecked(false);	    	        		
	        	}
	        	else
	        	{
	           		if(coyote_Group.getCheckedRadioButtonId()==R.id.coyotes_High)
	        		{
	        	    coyote_Group.clearCheck();
	        		}
	        		if(hawk_Group.getCheckedRadioButtonId()==R.id.hawks_High)
	        		{
	        			hawk_Group.clearCheck();
	        		}
	        		if(grain_Group.getCheckedRadioButtonId()==R.id.grains_High)
	        		{
	        			grain_Group.clearCheck();
	        		}
		    	}
	        }
	    });


	    dog_Group.setOnCheckedChangeListener(new OnCheckedChangeListener()
	    {
	        public void onCheckedChanged(RadioGroup buttonView, int checkedID)
	        {
	        	switch (checkedID){
	        	case R.id.dogs_None:
	        		newObs.setDogs("NONE");
	        		break;
	        	case R.id.dogs_Low:
	        		newObs.setDogs("LOW");
	        		break;
	        	case R.id.dogs_Medium:
	        		newObs.setDogs("MEDIUM");
	        		break;
	        	case R.id.dogs_High:
	        		newObs.setDogs("HIGH");
	        		break;
	        	default: break;
	        	}   
	        }
	    });
	    cat_Group.setOnCheckedChangeListener(new OnCheckedChangeListener()
	    {
	        public void onCheckedChanged(RadioGroup buttonView, int checkedID)
	        {
	        	switch (checkedID){
	        	case R.id.cats_None:
	        		newObs.setCats("NONE");
	        		break;
	        	case R.id.cats_Low:
	        		newObs.setCats("LOW");
	        		break;
	        	case R.id.cats_Medium:
	        		newObs.setCats("MEDIUM");
	        		break;
	        	case R.id.cats_High:
	        		newObs.setCats("HIGH");
	        		break;
	        	default: break;
	        	}   
	        }
	    });
	    coyote_Group.setOnCheckedChangeListener(new OnCheckedChangeListener()
	    {
	        public void onCheckedChanged(RadioGroup buttonView, int checkedID)
	        {
	        	switch (checkedID){
	        	case R.id.coyotes_None:
	        		newObs.setCoyotes("NONE");
	        		break;
	        	case R.id.coyotes_Low:
	        		newObs.setCoyotes("LOW");
	        		break;
	        	case R.id.coyotes_Medium:
	        		newObs.setCoyotes("MEDIUM");
	        		break;
	        	case R.id.coyotes_High:
	        		newObs.setCoyotes("HIGH");
	        		break;
	        	default: break;
	        	}   
	        }
	    });
	    hawk_Group.setOnCheckedChangeListener(new OnCheckedChangeListener()
	    {
	        public void onCheckedChanged(RadioGroup buttonView, int checkedID)
	        {
	        	switch (checkedID){
	        	case R.id.hawks_None:
	        		newObs.setHawks("NONE");
	        		break;
	        	case R.id.hawks_Low:
	        		newObs.setHawks("LOW");
	        		break;
	        	case R.id.hawks_Medium:
	        		newObs.setHawks("MEDIUM");
	        		break;
	        	case R.id.hawks_High:
	        		newObs.setHawks("HIGH");
	        		break;
	        	default: break;
	        	}   
	        }
	    });
	    grain_Group.setOnCheckedChangeListener(new OnCheckedChangeListener()
	    {
	        public void onCheckedChanged(RadioGroup buttonView, int checkedID)
	        {
	        	switch (checkedID){
	        	case R.id.grains_None:
	        		newObs.setPGrain("NONE");
	        		break;
	        	case R.id.grains_Low:
	        		newObs.setPGrain("LOW");
	        		break;
	        	case R.id.grains_Medium:
	        		newObs.setPGrain("MEDIUM");
	        		break;
	        	case R.id.grains_High:
	        		newObs.setPGrain("HIGH");
	        		break;
	        	default: break;
	        	}   
	        }
	    });
	    
	  	    
	    
	   
	    
	    
	 
		
		


		
		
	  



	    infoIconButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(NewObservation7.this, infoMenu.class);
		        startActivity(i);
			}	    	
	    });
	    
	    settingsIconButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(NewObservation7.this, settingsMenu.class);
		        startActivity(i);
			}	    	

	    });

	    
	  
	    nextObsButton7.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(NewObservation7.this, Confirmation.class);
		        startActivity(i);
				//finish();				
			}
			
	    });

	 

	
     }
     	@Override
	protected void onStart(){
     	// for the dialog indicating the gps is disabled
		super.onStart();
	}
	@Override
	protected void onDestroy() {
		//stopListening();
		super.onDestroy();
	}
	
	@Override
	protected void onPause() {
		//stopListening();
		super.onPause();
	}
	
	@Override
	protected void onResume() {
		//startListening();
		super.onResume();
	}
	

	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	    if ((keyCode == KeyEvent.KEYCODE_MENU) ) {
	        
	    }
	    return super.onKeyDown(keyCode, event);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
	super.onCreateOptionsMenu(menu);

	menu.add(PREFERENCES_GROUP_ID, PAGE_2, 0, "Date/Time")
	.setIcon(R.drawable.menuicondatetime);
	menu.add(PREFERENCES_GROUP_ID, PAGE_3, 0, "General")
	.setIcon(R.drawable.menuicongencom);
	menu.add(PREFERENCES_GROUP_ID, PAGE_4, 0, "Setting")
	.setIcon(R.drawable.menuiconsetting);
	menu.add(PREFERENCES_GROUP_ID, PAGE_5, 0, "Trees")
	.setIcon(R.drawable.menuicontree);
	menu.add(PREFERENCES_GROUP_ID, PAGE_6, 0, "Food")
	.setIcon(R.drawable.menuiconfood);
	menu.add(PREFERENCES_GROUP_ID, PAGE_7, 0, "Predators")
	.setIcon(R.drawable.menuiconpred);

	return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		Intent i;
		switch (item.getItemId()) {
		case PAGE_2:
			i = new Intent(NewObservation7.this, NewObservation2.class);
	        i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
	        startActivity(i);
	        break;
		case PAGE_3:
			i = new Intent(NewObservation7.this, NewObservation3.class);
	        i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
	        startActivity(i);
	        break;
		case PAGE_4:
			i = new Intent(NewObservation7.this, NewObservation4.class);
	        i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
	        startActivity(i);
	        break;
		case PAGE_5:
			i = new Intent(NewObservation7.this, NewObservation5.class);
	        i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
	        startActivity(i);
	        break;
	    case PAGE_6:
	    	i = new Intent(NewObservation7.this, NewObservation6.class);
	        i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
	        startActivity(i);
	        break;
		case PAGE_7:
			return true;
		}
		return true;
	}
	
}

